package com.ccxe.manage.util.help;

/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: wangyaoxi
 * Date: 2010-4-2 上午11:26:00
 */

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据库数据的Map转换类
 * 
 * @author wyx
 * 
 */
public class EntityHelper {

	/**
	 * wyx:取结果集的第一条数据
	 * 
	 * @param rs
	 * @return
	 */
	public Map<String, Object> getOneInRs(ResultSet rs) {
		ResultSetMetaData rsmd = null;
		try {
			rsmd = rs.getMetaData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return this.getOneInRs(rs, rsmd);

	}

	/**
	 * wyx:取结果集的第一条数据
	 * 
	 * @param rs
	 *            结果集
	 * @param rsmd
	 *            结果集头元素
	 * @return
	 */
	public Map<String, Object> getOneInRs(ResultSet rs, ResultSetMetaData rsmd) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 遍历头,取出结果集数据
			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				String columnName = rsmd.getColumnName(i);
				// 获取该列数据
				Object obj = rs.getObject(columnName);
				if (obj == null) {
					obj = new String("");
				}
				map.put(columnName, obj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;

	}

	/**
	 * wyx:结果集转换为list<Map>
	 * 
	 * @param rs
	 * @return
	 */
	public List<Map<?, ?>> getListInRs(ResultSet rs) {
		List<Map<?, ?>> list = new ArrayList<Map<?, ?>>();
		try {
			ResultSetMetaData rsmd = rs.getMetaData();
			while (rs.next()) {
				Map<?, ?> map = this.getOneInRs(rs, rsmd);
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return list;
	}
}
